so://phie

poppy

A feature-rich Minecraft plugin which enhances gaming experience
git clone git://soophie.de/poppy

blob: cd61a2f54ca41e75dd99a6fcb61e6541debbcf7e (4808 bytes)[doc][blame][raw]

1package de.typable.minecrafthub.event;

2

3import java.util.ArrayList;

4import java.util.Iterator;

5import java.util.List;

6import java.util.ListIterator;

7

8import org.bukkit.Bukkit;

9import org.bukkit.block.Block;

10import org.bukkit.block.BlockFace;

11import org.bukkit.block.Dropper;

12import org.bukkit.entity.Entity;

13import org.bukkit.entity.ItemFrame;

14import org.bukkit.event.EventHandler;

15import org.bukkit.event.Listener;

16import org.bukkit.event.inventory.InventoryMoveItemEvent;

17import org.bukkit.inventory.Inventory;

18import org.bukkit.inventory.ItemStack;

19import org.bukkit.inventory.Recipe;

20import org.bukkit.inventory.ShapedRecipe;

21import org.bukkit.inventory.ShapelessRecipe;

22

23import de.typable.minecrafthub.util.Util;

24

25

26public class AutoWorkbenchListener implements Listener

27{

28 @EventHandler

29 public void onItemMove(InventoryMoveItemEvent event)

30 {

31 Inventory source = event.getSource();

32 Inventory target = event.getDestination();

33

34 if(source.getHolder() instanceof Dropper)

35 {

36 Dropper dropper = (Dropper) source.getHolder();

37

38 ItemFrame frame = getItemFrame(dropper.getBlock());

39

40 if(frame == null)

41 {

42 return;

43 }

44

45 ItemStack item = frame.getItem();

46

47 Recipe recipe = getCraftingRecipe(item, source, target);

48

49 if(recipe != null)

50 {

51 if(recipe instanceof ShapelessRecipe)

52 {

53 ShapelessRecipe shapless = (ShapelessRecipe) recipe;

54

55 target.addItem(shapless.getResult());

56 }

57

58 if(recipe instanceof ShapedRecipe)

59 {

60 ShapedRecipe shaped = (ShapedRecipe) recipe;

61

62 target.addItem(shaped.getResult());

63 }

64 }

65

66 event.setCancelled(true);

67 }

68 }

69

70 private Recipe getCraftingRecipe(ItemStack item, Inventory source, Inventory target)

71 {

72 if(Util.isEmpty(item))

73 {

74 return null;

75 }

76

77 Iterator<Recipe> iterator = Bukkit.recipeIterator();

78

79 while(iterator.hasNext())

80 {

81 Recipe recipe = iterator.next();

82

83 if(Util.compare(item, recipe.getResult()))

84 {

85 List<ItemStack> ingredients = null;

86

87 if(recipe instanceof ShapelessRecipe)

88 {

89 ShapelessRecipe shapless = (ShapelessRecipe) recipe;

90

91 ingredients = shapless.getIngredientList();

92 }

93

94 if(recipe instanceof ShapedRecipe)

95 {

96 ShapedRecipe shaped = (ShapedRecipe) recipe;

97

98 ingredients = new ArrayList<>();

99

100 for(ItemStack value : shaped.getIngredientMap().values())

101 {

102 if(ingredients.isEmpty())

103 {

104 if(!Util.isEmpty(value))

105 {

106 ingredients.add(value);

107 }

108 }

109 else

110 {

111 appendUniqueIngredient(ingredients, value);

112 }

113 }

114 }

115

116 if(ingredients == null)

117 {

118 continue;

119 }

120

121 if(!hasIngredients(source, ingredients))

122 {

123 continue;

124 }

125

126 if(Util.isInventoryFull(target, recipe.getResult()))

127 {

128 return null;

129 }

130

131 removeIngredients(ingredients, source);

132

133 return recipe;

134 }

135 }

136

137 return null;

138 }

139

140 private void removeIngredients(List<ItemStack> ingredients, Inventory inventory)

141 {

142 for(ItemStack ingredient : ingredients)

143 {

144 if(Util.isEmpty(ingredient))

145 {

146 continue;

147 }

148

149 int amount = ingredient.getAmount();

150

151 ListIterator<ItemStack> iterator = inventory.iterator();

152

153 while(iterator.hasNext())

154 {

155 ItemStack item = iterator.next();

156

157 if(Util.isEmpty(item))

158 {

159 continue;

160 }

161

162 if(Util.compare(item, ingredient))

163 {

164 if(item.getAmount() > amount)

165 {

166 item.setAmount(item.getAmount() - amount);

167 amount = 0;

168

169 break;

170 }

171

172 if(item.getAmount() == amount)

173 {

174 amount = 0;

175 inventory.removeItem(item);

176

177 break;

178 }

179

180 if(item.getAmount() < amount)

181 {

182 amount -= item.getAmount();

183 inventory.removeItem(item);

184 }

185 }

186 }

187 }

188 }

189

190 private void appendUniqueIngredient(List<ItemStack> ingredients, ItemStack item)

191 {

192 for(ItemStack ingredient : ingredients)

193 {

194 if(Util.compare(item, ingredient))

195 {

196 ingredient.setAmount(ingredient.getAmount() + item.getAmount());

197

198 return;

199 }

200 }

201

202 if(!Util.isEmpty(item))

203 {

204 ingredients.add(item);

205 }

206 }

207

208 private boolean hasIngredients(Inventory inventory, List<ItemStack> ingredients)

209 {

210 for(ItemStack ingredient : ingredients)

211 {

212 if(ingredient != null && !Util.containsAtLeast(inventory, ingredient, ingredient.getAmount()))

213 {

214 return false;

215 }

216 }

217

218 return true;

219 }

220

221 private ItemFrame getItemFrame(Block block)

222 {

223 for(Entity entity : block.getWorld().getNearbyEntities(block.getLocation(), 2, 2, 2))

224 {

225 if(entity instanceof ItemFrame)

226 {

227 ItemFrame frame = (ItemFrame) entity;

228

229 if(frame.getFacing() == BlockFace.UP)

230 {

231 if(block.getLocation().add(0.5, 1.03125, 0.5).distance(frame.getLocation()) < 0.2D)

232 {

233 return frame;

234 }

235 }

236 }

237 }

238

239 return null;

240 }

241}