From cc1fb344e8731f975b5bebec6f931c5b888248f7 Mon Sep 17 00:00:00 2001 From: kegare Date: Tue, 22 Mar 2016 10:14:14 +0900 Subject: [PATCH 1/2] =?UTF-8?q?=E6=96=B0=E3=81=97=E3=81=84ShopAPI=E3=81=AE?= =?UTF-8?q?=E5=AE=9F=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit MODIDと紐付けて管理することで、マルチプレイにおける構成の違う環境でも、安全に開けるようになります。 本実装する場合は、新しいOpenShopGuiEventを実装もしくは修正する必要があります。 --- .../java/shift/mceconomy2/MCEconomy2.java | 4 + .../shift/mceconomy2/api/shop/IShopAPI.java | 19 ++ .../shift/mceconomy2/api/shop/ShopAPI.java | 93 +++++++ .../java/shift/mceconomy2/gui/GuiNewShop.java | 226 ++++++++++++++++++ .../mceconomy2/packet/MessageOpenShop.java | 48 ++++ .../mceconomy2/packet/PacketHandler.java | 2 + .../mceconomy2/packet/PacketOpenShop.java | 50 ++++ .../shift/mceconomy2/shop/ShopAPIHandler.java | 113 +++++++++ 8 files changed, 555 insertions(+) create mode 100644 src/main/java/shift/mceconomy2/api/shop/IShopAPI.java create mode 100644 src/main/java/shift/mceconomy2/api/shop/ShopAPI.java create mode 100644 src/main/java/shift/mceconomy2/gui/GuiNewShop.java create mode 100644 src/main/java/shift/mceconomy2/packet/MessageOpenShop.java create mode 100644 src/main/java/shift/mceconomy2/packet/PacketOpenShop.java create mode 100644 src/main/java/shift/mceconomy2/shop/ShopAPIHandler.java diff --git a/src/main/java/shift/mceconomy2/MCEconomy2.java b/src/main/java/shift/mceconomy2/MCEconomy2.java index aca0d84..fd6fda8 100644 --- a/src/main/java/shift/mceconomy2/MCEconomy2.java +++ b/src/main/java/shift/mceconomy2/MCEconomy2.java @@ -8,12 +8,14 @@ import cpw.mods.fml.common.network.NetworkRegistry; import net.minecraftforge.common.MinecraftForge; import shift.mceconomy2.api.MCEconomyAPI; +import shift.mceconomy2.api.shop.ShopAPI; import shift.mceconomy2.event.CommonEventManager; import shift.mceconomy2.event.MPManager; import shift.mceconomy2.gui.HUDMP; import shift.mceconomy2.item.MCEItems; import shift.mceconomy2.packet.PacketHandler; import shift.mceconomy2.proxy.CommonProxy; +import shift.mceconomy2.shop.ShopAPIHandler; @Mod(modid = MCEconomy2.MODID, version = MCEconomy2.VERSION, dependencies = MCEconomy2.DEPENDENCY) public class MCEconomy2 { @@ -46,6 +48,8 @@ public void preInit(FMLPreInitializationEvent event) { MCEconomyAPI.ShopManager = new ShopManager(); MCEconomyAPI.registerPurchaseItem(); + ShopAPI.apiHandler = new ShopAPIHandler(); + } @EventHandler diff --git a/src/main/java/shift/mceconomy2/api/shop/IShopAPI.java b/src/main/java/shift/mceconomy2/api/shop/IShopAPI.java new file mode 100644 index 0000000..69617b5 --- /dev/null +++ b/src/main/java/shift/mceconomy2/api/shop/IShopAPI.java @@ -0,0 +1,19 @@ +package shift.mceconomy2.api.shop; + +import java.util.List; +import java.util.Map; + +import net.minecraft.entity.player.EntityPlayer; + +public interface IShopAPI { + + public int registerShop(String modid, IShop shop); + + public IShop getShop(String modid, int id); + + public List getShops(String modid); + + public Map> getShopEntries(); + + public void openShop(EntityPlayer player, String modid, int id); +} \ No newline at end of file diff --git a/src/main/java/shift/mceconomy2/api/shop/ShopAPI.java b/src/main/java/shift/mceconomy2/api/shop/ShopAPI.java new file mode 100644 index 0000000..c78fbba --- /dev/null +++ b/src/main/java/shift/mceconomy2/api/shop/ShopAPI.java @@ -0,0 +1,93 @@ +package shift.mceconomy2.api.shop; + +import java.util.Collections; +import java.util.List; +import java.util.Map; + +import net.minecraft.entity.player.EntityPlayer; + +/** + * MOD別にショップを管理します。
+ * MODIDごとにショップリストが作成されます。 + * @author kegare + */ +public final class ShopAPI { + + public static IShopAPI apiHandler; + + /** + * ショップを登録します。 + * @param modid 登録するMODのID + * @param shop 登録するショップ + * @return MOD別のショップIDを返します。登録に失敗した場合は、-1を返します。 + */ + public static int registerShop(String modid, IShop shop) { + + if (apiHandler != null) { + + return apiHandler.registerShop(modid, shop); + } + + return -1; + } + + /** + * ショップを取得します。 + * @param modid ショップのMODID + * @param id ショップID + * @return ショップが取得できなかった場合は、nullを返します。 + */ + public static IShop getShop(String modid, int id) { + + if (apiHandler != null) { + + return apiHandler.getShop(modid, id); + } + + return null; + } + + /** + * ショップリストを取得します。 + * @return ショップリストが取得できなかった場合は、不変の空のリストを返します。 + */ + public static List getShops(String modid) { + + if (apiHandler != null) { + + return apiHandler.getShops(modid); + } + + return Collections.emptyList(); + } + + /** + * ショップリストのエントリマップを取得します。
+ * マップのキーにはMODID、値には対応するショップリストが格納されています。 + */ + public static Map> getShopEntries() { + + if (apiHandler != null) { + + return apiHandler.getShopEntries(); + } + + return Collections.emptyMap(); + } + + /** + * ショップを開きます。
+ * クライアントからでもサーバーからでも呼び出すことができます。
+ * 片側のみで呼び出すようにしてください。 + * @param player プレイヤー + * @param modid ショップのMODID + * @param id ショップID + */ + public static void openShop(EntityPlayer player, String modid, int id) { + + if (apiHandler != null) { + + apiHandler.openShop(player, modid, id); + } + } +} \ No newline at end of file diff --git a/src/main/java/shift/mceconomy2/gui/GuiNewShop.java b/src/main/java/shift/mceconomy2/gui/GuiNewShop.java new file mode 100644 index 0000000..b9852f3 --- /dev/null +++ b/src/main/java/shift/mceconomy2/gui/GuiNewShop.java @@ -0,0 +1,226 @@ +package shift.mceconomy2.gui; + +import java.util.List; + +import org.lwjgl.opengl.GL11; +import org.lwjgl.opengl.GL12; + +import net.minecraft.client.gui.GuiButton; +import net.minecraft.client.gui.inventory.GuiContainer; +import net.minecraft.client.renderer.RenderHelper; +import net.minecraft.client.resources.I18n; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.item.ItemStack; +import net.minecraft.util.ResourceLocation; +import net.minecraft.world.World; +import shift.mceconomy2.api.MCEconomyAPI; +import shift.mceconomy2.api.shop.IProduct; +import shift.mceconomy2.api.shop.IShop; +import shift.mceconomy2.packet.PacketHandler; +import shift.mceconomy2.packet.PacketShopButton; + +public class GuiNewShop extends GuiContainer { + + private static final ResourceLocation merchantGuiTextures = new ResourceLocation("mceconomy2:textures/guis/villager.png"); + + private final IShop shop; + + private GuiButton nextButton; + private GuiButton prevButton; + + private int currentIndex; + + public GuiNewShop(EntityPlayer player, IShop shop, int id, World world) { + super(new ContainerShop(player, shop, world)); + this.shop = shop; + } + + @Override + public void initGui() { + super.initGui(); + + allowUserInput = true; + + if (nextButton == null) { + + nextButton = new GuiButtonMerchant(1, 0, 0, true); + nextButton.enabled = false; + } + + nextButton.xPosition = guiLeft + 154; + nextButton.yPosition = guiTop + 3; + + if (prevButton == null) { + + prevButton = new GuiButtonMerchant(2, 0, 0, false); + prevButton.enabled = false; + } + + prevButton.xPosition = guiLeft + 9; + prevButton.yPosition = nextButton.yPosition; + + buttonList.clear(); + buttonList.add(nextButton); + buttonList.add(prevButton); + } + + @Override + protected void drawGuiContainerForegroundLayer(int par1, int par2) { + + fontRendererObj.drawString(I18n.format(shop.getShopName(mc.theWorld, mc.thePlayer)), 28, ySize - 159 + 2, 4210752); + fontRendererObj.drawString(I18n.format("container.inventory"), 8, ySize - 96 + 2, 4210752); + + List products = shop.getProductList(mc.theWorld, mc.thePlayer); + + if (products != null && !products.isEmpty()) { + + IProduct product = products.get(currentIndex); + + if (product != null) { + + fontRendererObj.drawString("Cost", 46, 25, 4210752); + fontRendererObj.drawString(product.getCost(shop, mc.theWorld, mc.thePlayer) + " MP", 46, 34, 4210752); + } + } + } + + @Override + public void updateScreen() { + super.updateScreen(); + + List products = shop.getProductList(mc.theWorld, mc.thePlayer); + + if (products != null && !products.isEmpty()) { + + nextButton.enabled = currentIndex < products.size() - 1; + prevButton.enabled = currentIndex > 0; + } + } + + @Override + protected void actionPerformed(GuiButton button) { + + if (!button.enabled) { + + return; + } + + boolean flag = false; + + if (button.id == nextButton.id) { + + ++currentIndex; + + flag = true; + } + else if (button.id == prevButton.id) { + + --currentIndex; + + flag = true; + } + + if (flag) { + + ((ContainerShop)inventorySlots).setCurrentRecipeIndex(currentIndex); + + PacketHandler.INSTANCE.sendToServer(new PacketShopButton(currentIndex)); + } + } + + @Override + protected void drawGuiContainerBackgroundLayer(float par1, int par2, int par3) { + + GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F); + mc.getTextureManager().bindTexture(merchantGuiTextures); + drawTexturedModalRect(guiLeft, guiTop, 0, 0, xSize, ySize); + + List products = shop.getProductList(mc.theWorld, mc.thePlayer); + + if (products != null && !products.isEmpty()) { + + IProduct product = products.get(currentIndex); + + if (product != null) { + + int money = MCEconomyAPI.getPlayerMP(mc.thePlayer); + + if (money < product.getCost(shop, mc.theWorld, mc.thePlayer) || !product.canBuy(shop, mc.theWorld, mc.thePlayer)) + { + mc.getTextureManager().bindTexture(merchantGuiTextures); + GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F); + GL11.glDisable(GL11.GL_LIGHTING); + drawTexturedModalRect(guiLeft + 101, guiTop + 29, 212, 0, 28, 21); + drawTexturedModalRect(guiLeft + 134, guiTop + 27, 176, 38, 30, 30); + } + } + } + + mc.getTextureManager().bindTexture(HUDMP.icons); + + int i = 93 - 86; + + drawTexturedModalRect(guiLeft + 83 - i, guiTop + 64, 0, 27, 86 + i, 15); + drawTexturedModalRect(guiLeft + 86 - i, guiTop + 67, 9, 0, 9, 9); + + int j = -3; + + drawTexturedModalRect(guiLeft + 86 + 65 + j, guiTop + 67, 0, 18, 9, 9); + drawTexturedModalRect(guiLeft + 86 + 74 + j, guiTop + 67, 9, 18, 9, 9); + + int money = MCEconomyAPI.getPlayerMP(mc.thePlayer); + int k = guiLeft; + + k += 86 + 56 + j; + + for (int l = 1; l <= String.valueOf(money).length(); l += 1) { + + String s = String.valueOf(money).substring(String.valueOf(money).length() - l, String.valueOf(money).length() - l + 1); + drawTexturedModalRect(k, guiTop + 67, 9 * Integer.parseInt(s), 9, 9, 9); + + k -= 8; + } + } + + @Override + public void drawScreen(int par1, int par2, float par3) { + super.drawScreen(par1, par2, par3); + + List products = shop.getProductList(mc.theWorld, mc.thePlayer); + + if (products != null && !products.isEmpty()) { + + IProduct product = products.get(currentIndex); + + if (product != null) { + + ItemStack itemstack = product.getItem(shop, mc.theWorld, mc.thePlayer); + + if (itemstack != null) { + + GL11.glPushMatrix(); + RenderHelper.enableGUIStandardItemLighting(); + GL11.glDisable(GL11.GL_LIGHTING); + GL11.glEnable(GL12.GL_RESCALE_NORMAL); + GL11.glEnable(GL11.GL_COLOR_MATERIAL); + GL11.glEnable(GL11.GL_LIGHTING); + itemRender.zLevel = 100.0F; + itemRender.renderItemAndEffectIntoGUI(this.fontRendererObj, this.mc.getTextureManager(), itemstack, guiLeft + 17, guiTop + 32); + itemRender.renderItemOverlayIntoGUI(this.fontRendererObj, this.mc.getTextureManager(), itemstack, guiLeft + 17, guiTop + 32); + itemRender.zLevel = 0.0F; + GL11.glDisable(GL11.GL_LIGHTING); + + if (func_146978_c(8, 23, 34, 34, par1, par2)) { + + renderToolTip(itemstack, par1, par2); + } + + GL11.glPopMatrix(); + GL11.glEnable(GL11.GL_LIGHTING); + GL11.glEnable(GL11.GL_DEPTH_TEST); + RenderHelper.enableStandardItemLighting(); + } + } + } + } +} \ No newline at end of file diff --git a/src/main/java/shift/mceconomy2/packet/MessageOpenShop.java b/src/main/java/shift/mceconomy2/packet/MessageOpenShop.java new file mode 100644 index 0000000..3ff2fa2 --- /dev/null +++ b/src/main/java/shift/mceconomy2/packet/MessageOpenShop.java @@ -0,0 +1,48 @@ +package shift.mceconomy2.packet; + +import cpw.mods.fml.common.FMLCommonHandler; +import cpw.mods.fml.common.network.simpleimpl.IMessage; +import cpw.mods.fml.common.network.simpleimpl.IMessageHandler; +import cpw.mods.fml.common.network.simpleimpl.MessageContext; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.entity.player.EntityPlayerMP; +import shift.mceconomy2.MCEconomy2; +import shift.mceconomy2.api.shop.IShop; +import shift.mceconomy2.gui.ContainerShop; +import shift.mceconomy2.gui.GuiNewShop; + +public class MessageOpenShop implements IMessageHandler { + + @Override + public IMessage onMessage(PacketOpenShop message, MessageContext ctx) { + + IShop shop = message.getShop(); + + if (shop == null) { + + return null; + } + + if (ctx.side.isServer()) { + + EntityPlayerMP player = ctx.getServerHandler().playerEntity; + ContainerShop container = new ContainerShop(player, shop, player.worldObj); + + player.getNextWindowId(); + player.closeContainer(); + int windowId = player.currentWindowId; + PacketHandler.INSTANCE.sendTo(new PacketOpenShop(message.getModID(), message.getShopID()), player); + player.openContainer = container; + player.openContainer.windowId = windowId; + player.openContainer.addCraftingToCrafters(player); + } + else { + + EntityPlayer player = MCEconomy2.proxy.getClientPlayer(); + + FMLCommonHandler.instance().showGuiScreen(new GuiNewShop(player, shop, message.getShopID(), player.worldObj)); + } + + return null; + } +} \ No newline at end of file diff --git a/src/main/java/shift/mceconomy2/packet/PacketHandler.java b/src/main/java/shift/mceconomy2/packet/PacketHandler.java index 5b3722d..be0e48b 100644 --- a/src/main/java/shift/mceconomy2/packet/PacketHandler.java +++ b/src/main/java/shift/mceconomy2/packet/PacketHandler.java @@ -20,6 +20,8 @@ public static void init(FMLPreInitializationEvent event) { INSTANCE.registerMessage(MessagePlayerProperties.class,MessagePlayerProperties.class, 0, Side.CLIENT); INSTANCE.registerMessage(ShopButtonHandler.class,PacketShopButton.class, 1, Side.SERVER); INSTANCE.registerMessage(MessageGuiId.class,PacketGuiId.class, 2, Side.SERVER); + INSTANCE.registerMessage(MessageOpenShop.class, PacketOpenShop.class, 3, Side.SERVER); + INSTANCE.registerMessage(MessageOpenShop.class, PacketOpenShop.class, 4, Side.CLIENT); } diff --git a/src/main/java/shift/mceconomy2/packet/PacketOpenShop.java b/src/main/java/shift/mceconomy2/packet/PacketOpenShop.java new file mode 100644 index 0000000..1712a47 --- /dev/null +++ b/src/main/java/shift/mceconomy2/packet/PacketOpenShop.java @@ -0,0 +1,50 @@ +package shift.mceconomy2.packet; + +import cpw.mods.fml.common.network.ByteBufUtils; +import cpw.mods.fml.common.network.simpleimpl.IMessage; +import io.netty.buffer.ByteBuf; +import shift.mceconomy2.api.shop.IShop; +import shift.mceconomy2.api.shop.ShopAPI; + +public class PacketOpenShop implements IMessage { + + private String modid; + private int id; + + public PacketOpenShop() {} + + public PacketOpenShop(String modid, int id) { + + this.modid = modid; + this.id = id; + } + + @Override + public void fromBytes(ByteBuf buf) { + + modid = ByteBufUtils.readUTF8String(buf); + id = buf.readInt(); + } + + @Override + public void toBytes(ByteBuf buf) { + + ByteBufUtils.writeUTF8String(buf, modid); + buf.writeInt(id); + } + + public String getModID() { + + return modid; + } + + public int getShopID() { + + return id; + } + + public IShop getShop() { + + return ShopAPI.getShop(modid, id); + } +} \ No newline at end of file diff --git a/src/main/java/shift/mceconomy2/shop/ShopAPIHandler.java b/src/main/java/shift/mceconomy2/shop/ShopAPIHandler.java new file mode 100644 index 0000000..ad6e9a5 --- /dev/null +++ b/src/main/java/shift/mceconomy2/shop/ShopAPIHandler.java @@ -0,0 +1,113 @@ +package shift.mceconomy2.shop; + +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentMap; + +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; + +import cpw.mods.fml.common.FMLLog; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.entity.player.EntityPlayerMP; +import shift.mceconomy2.api.shop.IShop; +import shift.mceconomy2.api.shop.IShopAPI; +import shift.mceconomy2.gui.ContainerShop; +import shift.mceconomy2.packet.PacketHandler; +import shift.mceconomy2.packet.PacketOpenShop; + +public class ShopAPIHandler implements IShopAPI { + + private final ConcurrentMap> SHOP_MAP = Maps.newConcurrentMap(); + + @Override + public int registerShop(String modid, IShop shop) { + + List shops = SHOP_MAP.get(modid); + + if (shops == null) { + + shops = Lists.newArrayList(); + } + + if (shops.add(shop)) { + + SHOP_MAP.putIfAbsent(modid, shops); + + return shops.indexOf(shop); + } + + return -1; + } + + @Override + public IShop getShop(String modid, int id) { + + List shops = SHOP_MAP.get(modid); + + if (shops == null) { + + FMLLog.warning("MCEconomy: Shop list does not exist!"); + + return null; + } + + if (id < 0 || id >= shops.size()) { + + FMLLog.warning("MCEconomy: This shop id is invalid!"); + + return null; + } + + IShop shop = shops.get(id); + + if (shop == null) { + + FMLLog.warning("MCEconomy: Shop does not exist!"); + } + + return shop; + } + + @Override + public List getShops(String modid) { + + List shops = SHOP_MAP.get(modid); + + if (shops == null) { + + return Collections.emptyList(); + } + + return shops; + } + + @Override + public Map> getShopEntries() { + + return SHOP_MAP; + } + + @Override + public void openShop(EntityPlayer player, String modid, int id) { + + if (player.worldObj.isRemote) { + + PacketHandler.INSTANCE.sendToServer(new PacketOpenShop(modid, id)); + } + else if (player instanceof EntityPlayerMP) { + + EntityPlayerMP thePlayer = (EntityPlayerMP)player; + ContainerShop container = new ContainerShop(thePlayer, getShop(modid, id), thePlayer.worldObj); + + thePlayer.getNextWindowId(); + thePlayer.closeContainer(); + int windowId = thePlayer.currentWindowId; + PacketHandler.INSTANCE.sendTo(new PacketOpenShop(modid, id), thePlayer); + thePlayer.openContainer = container; + thePlayer.openContainer.windowId = windowId; + thePlayer.openContainer.addCraftingToCrafters(thePlayer); + } + } +} \ No newline at end of file From f24d278dfba1b90058ba7a7051d0b1787bfe57a9 Mon Sep 17 00:00:00 2001 From: kegare Date: Tue, 22 Mar 2016 18:25:09 +0900 Subject: [PATCH 2/2] =?UTF-8?q?ShopEvent=E3=81=AE=E5=AE=9F=E8=A3=85?= =?UTF-8?q?=E3=80=81=E5=BE=93=E6=9D=A5=E3=81=AE=E3=82=B7=E3=83=A7=E3=83=83?= =?UTF-8?q?=E3=83=97=E7=AE=A1=E7=90=86=E3=82=92=E9=9D=9E=E6=8E=A8=E5=A5=A8?= =?UTF-8?q?=E3=81=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/shift/mceconomy2/ShopManager.java | 57 +++++-------------- .../shift/mceconomy2/api/MCEconomyAPI.java | 3 + .../shift/mceconomy2/api/event/ShopEvent.java | 52 +++++++++++++++++ .../shift/mceconomy2/api/shop/ShopAPI.java | 2 +- .../java/shift/mceconomy2/gui/GuiNewShop.java | 2 +- .../mceconomy2/packet/MessageOpenShop.java | 2 +- .../shift/mceconomy2/shop/ShopAPIHandler.java | 16 ++++++ 7 files changed, 87 insertions(+), 47 deletions(-) create mode 100644 src/main/java/shift/mceconomy2/api/event/ShopEvent.java diff --git a/src/main/java/shift/mceconomy2/ShopManager.java b/src/main/java/shift/mceconomy2/ShopManager.java index df24807..e8ac0f9 100644 --- a/src/main/java/shift/mceconomy2/ShopManager.java +++ b/src/main/java/shift/mceconomy2/ShopManager.java @@ -5,6 +5,8 @@ import java.util.Iterator; import java.util.Map.Entry; +import com.google.common.collect.Lists; + import net.minecraft.entity.Entity; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemStack; @@ -15,30 +17,21 @@ import shift.mceconomy2.api.shop.IProductList; import shift.mceconomy2.api.shop.IShop; import shift.mceconomy2.api.shop.IShopManager; +import shift.mceconomy2.api.shop.ShopAPI; import shift.mceconomy2.api.shop.ShopAdapter; -import shift.mceconomy2.packet.PacketGuiId; -import shift.mceconomy2.packet.PacketHandler; public class ShopManager implements IShopManager { //private final ArrayList ProductList = new ArrayList(); - private final ArrayList shopList = new ArrayList(); - private static final HashMap purchaseList = new HashMap(); private static final HashMap purchaseFluidList = new HashMap(); private static final HashMap, Integer> purchaseEntityList = new HashMap, Integer>(); - public ShopManager() { - - shopList.add(null); - shopList.add(null); - - } + //ショップ関係 - //shop関係 @Override public int registerProductList(IProductList list) { @@ -68,51 +61,27 @@ public ArrayList getProductLists() { } @Override - public int registerShop(IShop list) { - - shopList.add(list); - - return shopList.indexOf(list); + public int registerShop(IShop shop) { + return ShopAPI.registerShop("deprecated", shop); } @Override public IShop getShop(int id) { - if (shopList.size() < id) { - return null; - } - return shopList.get(id); + return ShopAPI.getShop("deprecated", id); } @Override public ArrayList getShops() { - return shopList; - } - //ショップ関係 + return Lists.newArrayList(ShopAPI.getShops("deprecated")); + } @Override public void openShopGui(int id, EntityPlayer player, World world, int x, int y, int z) { - /*OpenShopGuiEvent event = new OpenShopGuiEvent(player, id, world, x, y, z); - - if (MinecraftForge.EVENT_BUS.post(event)) - { - return; - } - - if (event.getResult() == Result.ALLOW) - { - return ; - }*/ - - if (world.isRemote) { - PacketHandler.INSTANCE.sendToServer(new PacketGuiId(id)); - } else { - player.openGui(MCEconomy2.instance, id, world, x, y, z); - } - + ShopAPI.openShop(player, "deprecated", id); } @Override @@ -156,7 +125,7 @@ private boolean func_151397_a(ItemStack p_151397_1_, ItemStack p_151397_2_) { @Override public boolean hasPurchase(ItemStack item) { - return (this.getPurchase(item) != -1 && this.getPurchase(item) != -2); + return this.getPurchase(item) != -1 && this.getPurchase(item) != -2; } @Override @@ -180,7 +149,7 @@ public double getFluidPurchase(Fluid fluid) { @Override public boolean hasFluidPurchase(Fluid fluid) { - return (this.getFluidPurchase(fluid) != -1 && this.getFluidPurchase(fluid) != -2); + return this.getFluidPurchase(fluid) != -1 && this.getFluidPurchase(fluid) != -2; } @Override @@ -206,7 +175,7 @@ public int getEntityPurchase(Entity entity) { @Override public boolean hasEntityPurchase(Entity entity) { - return (this.getEntityPurchase(entity) != -1 && this.getEntityPurchase(entity) != -2); + return this.getEntityPurchase(entity) != -1 && this.getEntityPurchase(entity) != -2; } } diff --git a/src/main/java/shift/mceconomy2/api/MCEconomyAPI.java b/src/main/java/shift/mceconomy2/api/MCEconomyAPI.java index 23c15d6..ac698cd 100644 --- a/src/main/java/shift/mceconomy2/api/MCEconomyAPI.java +++ b/src/main/java/shift/mceconomy2/api/MCEconomyAPI.java @@ -150,6 +150,7 @@ public static IProductList getProductList(int id) { * @see IShop * @return ShopID */ + @Deprecated public static int registerShop(IShop shop) { return ShopManager.registerShop(shop); } @@ -159,6 +160,7 @@ public static int registerShop(IShop shop) { * @param id ShopID * @return IShop */ + @Deprecated public static IShop getShop(int id) { return ShopManager.getShop(id); } @@ -172,6 +174,7 @@ public static IShop getShop(int id) { * @param y y軸 * @param z z軸 */ + @Deprecated public static void openShopGui(int id, EntityPlayer player, World world, int x, int y, int z) { ShopManager.openShopGui(id, player, world, x, y, z); } diff --git a/src/main/java/shift/mceconomy2/api/event/ShopEvent.java b/src/main/java/shift/mceconomy2/api/event/ShopEvent.java new file mode 100644 index 0000000..5d0dc3f --- /dev/null +++ b/src/main/java/shift/mceconomy2/api/event/ShopEvent.java @@ -0,0 +1,52 @@ +package shift.mceconomy2.api.event; + +import cpw.mods.fml.common.eventhandler.Cancelable; +import cpw.mods.fml.common.eventhandler.Event; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraftforge.common.MinecraftForge; +import shift.mceconomy2.api.shop.IShop; + +public class ShopEvent extends Event { + + public final IShop shop; + + public ShopEvent(IShop shop) { + this.shop = shop; + } + + /** + * ショップが登録される前に呼ばれるイベントです。
+ * このイベントをキャンセルすると、そのショップは登録されません。
+ * このイベントは、{@link MinecraftForge#EVENT_BUS}で呼ばれます。 + */ + @Cancelable + public static class Register extends ShopEvent { + + public final String modId; + + public Register(String modId, IShop shop) { + super(shop); + this.modId = modId; + } + } + + /** + * ショップが開かれる前に呼ばれるイベントです。
+ * このイベントをキャンセルすると、そのショップは開かれません。
+ * このイベントは、{@link MinecraftForge#EVENT_BUS}で呼ばれます。 + */ + @Cancelable + public static class OpenShop extends ShopEvent { + + public final EntityPlayer player; + public final String modId; + public final int shopId; + + public OpenShop(EntityPlayer player, String modId, int shopId, IShop shop) { + super(shop); + this.player = player; + this.modId = modId; + this.shopId = shopId; + } + } +} \ No newline at end of file diff --git a/src/main/java/shift/mceconomy2/api/shop/ShopAPI.java b/src/main/java/shift/mceconomy2/api/shop/ShopAPI.java index c78fbba..be99dc1 100644 --- a/src/main/java/shift/mceconomy2/api/shop/ShopAPI.java +++ b/src/main/java/shift/mceconomy2/api/shop/ShopAPI.java @@ -8,7 +8,7 @@ /** * MOD別にショップを管理します。
- * MODIDごとにショップリストが作成されます。 + * MODIDごとにショップリストが生成されます。 * @author kegare */ public final class ShopAPI { diff --git a/src/main/java/shift/mceconomy2/gui/GuiNewShop.java b/src/main/java/shift/mceconomy2/gui/GuiNewShop.java index b9852f3..2bf7dd4 100644 --- a/src/main/java/shift/mceconomy2/gui/GuiNewShop.java +++ b/src/main/java/shift/mceconomy2/gui/GuiNewShop.java @@ -30,7 +30,7 @@ public class GuiNewShop extends GuiContainer { private int currentIndex; - public GuiNewShop(EntityPlayer player, IShop shop, int id, World world) { + public GuiNewShop(EntityPlayer player, IShop shop, World world) { super(new ContainerShop(player, shop, world)); this.shop = shop; } diff --git a/src/main/java/shift/mceconomy2/packet/MessageOpenShop.java b/src/main/java/shift/mceconomy2/packet/MessageOpenShop.java index 3ff2fa2..455d2d6 100644 --- a/src/main/java/shift/mceconomy2/packet/MessageOpenShop.java +++ b/src/main/java/shift/mceconomy2/packet/MessageOpenShop.java @@ -40,7 +40,7 @@ public IMessage onMessage(PacketOpenShop message, MessageContext ctx) { EntityPlayer player = MCEconomy2.proxy.getClientPlayer(); - FMLCommonHandler.instance().showGuiScreen(new GuiNewShop(player, shop, message.getShopID(), player.worldObj)); + FMLCommonHandler.instance().showGuiScreen(new GuiNewShop(player, shop, player.worldObj)); } return null; diff --git a/src/main/java/shift/mceconomy2/shop/ShopAPIHandler.java b/src/main/java/shift/mceconomy2/shop/ShopAPIHandler.java index ad6e9a5..b946e33 100644 --- a/src/main/java/shift/mceconomy2/shop/ShopAPIHandler.java +++ b/src/main/java/shift/mceconomy2/shop/ShopAPIHandler.java @@ -11,6 +11,8 @@ import cpw.mods.fml.common.FMLLog; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayerMP; +import net.minecraftforge.common.MinecraftForge; +import shift.mceconomy2.api.event.ShopEvent; import shift.mceconomy2.api.shop.IShop; import shift.mceconomy2.api.shop.IShopAPI; import shift.mceconomy2.gui.ContainerShop; @@ -24,6 +26,13 @@ public class ShopAPIHandler implements IShopAPI { @Override public int registerShop(String modid, IShop shop) { + ShopEvent.Register event = new ShopEvent.Register(modid, shop); + + if (MinecraftForge.EVENT_BUS.post(event)) { + + return -1; + } + List shops = SHOP_MAP.get(modid); if (shops == null) { @@ -92,6 +101,13 @@ public Map> getShopEntries() { @Override public void openShop(EntityPlayer player, String modid, int id) { + ShopEvent.OpenShop event = new ShopEvent.OpenShop(player, modid, id, getShop(modid, id)); + + if (MinecraftForge.EVENT_BUS.post(event)) { + + return; + } + if (player.worldObj.isRemote) { PacketHandler.INSTANCE.sendToServer(new PacketOpenShop(modid, id));